package cn.stylefeng.guns.modular.api.service;

import cn.stylefeng.guns.core.common.constant.DatasourceEnum;
import cn.stylefeng.guns.modular.api.entity.Article;
import cn.stylefeng.guns.modular.api.mapper.ArticleMapper;
import cn.stylefeng.roses.core.mutidatasource.annotion.DataSource;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class IArticleService extends ServiceImpl<ArticleMapper,Article> {

    @Resource
    private ArticleMapper articleMapper;

    String[] colums = new String[]{"id","title","author","published_at","thumbnail_s1","thumbnail_s2","thumbnail_s3",
        "created_at","updated_at"};


    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Object list(int num) {
        IPage<Article> page =  baseMapper.selectPage(
                new Page<>(num,10),new QueryWrapper<>(
                        new Article(),colums)
        );
        return setThumbnailNum(page);
    }
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public Article get(int id) {
        return articleMapper.findDetailById(id);
    }


    private  static IPage<Article> setThumbnailNum(IPage<Article> page){
        List<Article> articles = page.getRecords();
        for (Article article:articles){
            if (StringUtils.isEmpty(article.getThumbnail_s1())){
                article.setThumbnailNum(0);
            }else {
                if (StringUtils.isEmpty(article.getThumbnail_s3())){
                    article.setThumbnailNum(1);
                }else {
                    article.setThumbnailNum(3);
                }
            }
        }
        return page;
    }




}
